﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Marbles.Core.Content.Sites;
using System.Web;

namespace Marbles.Core.Content.Templates
{
    public class Template
    {

        public static string DefaultTemplateValue
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("<%@ Master Language=\"C#\" AutoEventWireup=\"true\" %>");
                sb.AppendLine("<%@ Register TagPrefix=\"cms\" Namespace=\"Marbles.Core.Controls\" Assembly=\"Marbles.Core\" %>");
                sb.AppendLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
                sb.AppendLine("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
                sb.AppendLine("<head id=\"head\" runat=\"server\">");
                sb.AppendLine("<meta http-equiv=\"MSThemeCompatible\" content=\"Yes\">");
                sb.AppendLine("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=IE8\" />");
                sb.AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
                sb.AppendLine("<asp:ContentPlaceHolder ID=\"Header\" runat=\"server\">");
                sb.AppendLine("</asp:ContentPlaceHolder>");
                sb.AppendLine("</head>");
                sb.AppendLine("<body>");
                sb.AppendLine("<form id=\"mainForm\" runat=\"server\" enctype=\"multipart/form-data\">");
                sb.AppendLine("<cms:DynamicContentPlaceHolder ID=\"Body\" runat=\"server\">");
                sb.AppendLine("</cms:DynamicContentPlaceHolder>");
                sb.AppendLine("</form>");
                sb.AppendLine("</body>");
                sb.AppendLine("</html>");
                return sb.ToString();
            }

        }

        public static void CreateDefaultTemplate(SiteDefinition Web)
        {
            string filename = Path.Combine(HttpContext.Current.Server.MapPath(Web.TemplateDirectory), Web.Host + ".Master");
            if (!Directory.Exists(HttpContext.Current.Server.MapPath(Web.TemplateDirectory))) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath(Web.TemplateDirectory)); }

            if (File.Exists(filename)) { return; }
            StreamWriter sw = new StreamWriter(filename);
            sw.Write(DefaultTemplateValue);
            sw.Flush();
            sw.Close();
        }

        public static void CreateDefaultTheme(SiteDefinition Web)
        {
            string theme = Web.Theme;
            if (string.IsNullOrEmpty(theme))
            {
                theme = Web.SiteName.Replace(" ", "").Trim();
                Web.Theme = theme;
                Web.Update();
            }

            string path = HttpContext.Current.Server.MapPath("~/App_Themes/" + Web.Theme);
            if (!Directory.Exists(path)) { Directory.CreateDirectory(path); }

            CreateDefaultStylesheet(Path.Combine(path, "Style.css"));

        }

        private static void CreateDefaultStylesheet(string filename)
        {
            if (File.Exists(filename)) { return; }
            StreamWriter sw = new StreamWriter(filename);
            sw.Write(DefaultStyleSheetValue());
            sw.Flush();
            sw.Close();
        }

        private static string DefaultStyleSheetValue()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("html {");
            sb.AppendLine("}");
            sb.AppendLine("body {");
            sb.AppendLine("}");
            sb.AppendLine(".DropDownMenu {");
            sb.AppendLine("margin: 0;");
            sb.AppendLine("padding: 0;");
            sb.AppendLine("}");
            sb.AppendLine(".DropDownMenu li {");
            sb.AppendLine("float: left;");
            sb.AppendLine("list-style: none;");
            sb.AppendLine("font: 12px Tahoma, Arial;");
            sb.AppendLine("}");
            sb.AppendLine(".DropDownMenu li a {");
            sb.AppendLine("display: block;");
            sb.AppendLine("background: #20548E;");
            sb.AppendLine("padding: 5px 12px;");
            sb.AppendLine("text-decoration: none;");
            sb.AppendLine("border-right: 1px solid white;");
            sb.AppendLine("width: 70px;");
            sb.AppendLine("color: #EAFFED;");
            sb.AppendLine("white-space: nowrap;");
            sb.AppendLine("}");
            sb.AppendLine(".DropDownMenu li a:hover {");
            sb.AppendLine("background: #1A4473;");
            sb.AppendLine("}");
            sb.AppendLine(".DropDownMenu li ul {");
            sb.AppendLine("margin: 0;");
            sb.AppendLine("padding: 0;");
            sb.AppendLine("position: absolute;");
            sb.AppendLine("visibility: hidden;");
            sb.AppendLine("border-top: 1px solid white;");
            sb.AppendLine("}");
            sb.AppendLine(".DropDownMenu li ul li {");
            sb.AppendLine("float: none;");
            sb.AppendLine("display: inline;");
            sb.AppendLine("}");
            sb.AppendLine(".DropDownMenu li ul li a {");
            sb.AppendLine("width: auto;");
            sb.AppendLine("background: #1A4473;");
            sb.AppendLine("}");
            sb.AppendLine(".DropDownMenu li ul li a:hover {");
            sb.AppendLine("background: #7F1616;");
            sb.AppendLine("}");
            return sb.ToString();
        }
    }
}
